<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="mini" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="供应商编号" prop="supplierNo">
        <el-input v-model="queryParams.supplierNo" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商名称" prop="supplierName">
        <el-input v-model="queryParams.supplierName" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商类型" prop="supplierType">
        <el-select v-model="queryParams.supplierType" placeholder="请选择供方类型">
          <el-option v-for="dict in dict.type.erp_supplier_type" :key="dict.value" :label="dict.label"
            :value="parseInt(dict.value)"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="联系人" prop="linkman">
        <el-input v-model="queryParams.linkman" placeholder="请输入" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['erp:ErpSupplier:add']">新增供应商</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['erp:ErpSupplier:export']">导出列表</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ErpSupplierList" row-key="supplierId" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" align="center" :reserve-selection="true" width="50" />
      <el-table-column label="供应商编号" align="center" prop="supplierNo" sortable width="200" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" width="250"/>
      <el-table-column label="供方类别" align="center" prop="supplierType" sortable width="100" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_supplier_type" :value="scope.row.supplierType" />
        </template>
      </el-table-column>
      <el-table-column label="存货类型" align="center" prop="inventoryType" sortable width="100" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_inventory_type" :value="scope.row.inventoryType" />
        </template>
      </el-table-column>
      <el-table-column label="产品类型" align="center" prop="productType" sortable width="100" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_product_type" :value="scope.row.productType" />
        </template>
      </el-table-column>
      <el-table-column label="联系人" align="center" prop="linkman" width="80" />
      <el-table-column label="联系电话" align="center" prop="linkmanPhone" width="120" />
      <el-table-column label="开户银行" align="center" prop="bankName" width="80" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.erp_bank_name" :value="scope.row.bankName" />
        </template>
      </el-table-column>
      <el-table-column label="银行账号" align="center" prop="bankAccount" width="200" >
        <template slot-scope="scope">
          {{ scope.row.bankAccount ? scope.row.bankAccount.replace(/\s/g, '').replace(/[^\d]/g, '').replace(/(\d{4})(?=\d)/g, '$1 ') : '' }}
        </template>
      </el-table-column>
      <el-table-column label="纳税人识别号" align="center" prop="taxpayerNo" width="220" />
      <el-table-column label="供应商地址" prop="supplierAddress" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="220" >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)"
            v-hasPermi="['erp:ErpSupplier:edit']">修改</el-button>
          <el-button size="mini" type="text" @click="handleSetCommodity(scope.row)"
            v-hasPermi="['erp:ErpSupplierCommodity:add']">设置商品</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)"
            v-hasPermi="['erp:ErpSupplier:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改供应商管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="供应商编号" prop="supplierNo">
              <el-input v-model="form.supplierNo" :disabled="true" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供应商名称" prop="supplierName">
              <el-input v-model="form.supplierName" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="供方类型" prop="supplierType">
              <el-select v-model="form.supplierType" placeholder="请选择" @change="handleSupplierTypeChange">
                <el-option v-for="dict in dict.type.erp_supplier_type" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="存货类型" prop="inventoryType">
              <el-select v-model="form.inventoryType" placeholder="请选择" :disabled="form.supplierType == 2">
                <el-option v-for="dict in dict.type.erp_inventory_type" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品类型" prop="productType">
              <el-select v-model="form.productType" placeholder="请选择">
                <el-option v-for="dict in dict.type.erp_product_type" :key="dict.value" :label="dict.label"
                  :value="parseInt(dict.value)"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人" prop="linkman">
              <el-input v-model="form.linkman" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话" prop="linkmanPhone">
              <el-input v-model="form.linkmanPhone" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="开户银行" prop="bankName">
              <el-select v-model="form.bankName" placeholder="请选择">
                <el-option v-for="dict in dict.type.erp_bank_name" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="银行账号" prop="bankAccount">
              <el-input v-model="form.bankAccount" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="纳税人识别号" prop="taxpayerNo">
              <el-input v-model="form.taxpayerNo" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="供应商地址" prop="supplierAddress">
              <el-input v-model="form.supplierAddress" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4 }" v-model="form.remark"
                placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listErpSupplier,
  getErpSupplier,
  delErpSupplier,
  addErpSupplier,
  updateErpSupplier,
} from "@/api/erp/ErpSupplier";

import { validatePhone, bankAccountValid, isidentifyNumber } from '@/utils/validataForm'

export default {
  name: "ErpSupplier",
  dicts: [
    "erp_product_type",
    "sys_yes_no",
    "erp_inventory_type",
    "erp_supplier_type",
    "erp_bank_name",
  ],
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 供应商管理表格数据
      ErpSupplierList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        supplierNo: null,
        supplierName: null,
        supplierType: null,
        linkman: null,
        flag: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        supplierName: [
          { required: true, message: "供应商名称不能为空", trigger: "blur" },
        ],
        companyName: [
          { required: true, message: "公司名称不能为空", trigger: "blur" },
        ],
        supplierType: [
          { required: true, message: "供方类别不能为空", trigger: "blur" },
        ],
        inventoryType: [
          { required: true, message: "存货类型不能为空", trigger: "blur" },
        ],
        // productType: [
        //   { required: true, message: "产品类型不能为空", trigger: "blur" },
        // ],
        // linkman: [
        //   { required: true, message: "联系人不能为空", trigger: "blur" },
        // ],
        // linkmanPhone: [
        //   { required: true, trigger: "blur", validator: validatePhone },
        // ],
        // bankName: [
        //   { required: true, message: "开户银行不能为空", trigger: "blur" },
        // ],
        // bankAccount: [
        //   { required: true, trigger: "blur", validator: bankAccountValid },
        // ],
        // taxpayerNo: [
        //   { required: true, validator: isidentifyNumber, trigger: "blur" },
        // ],
        // supplierAddress: [
        //   { required: true, message: "供应商地址不能为空", trigger: "blur" },
        // ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询供应商管理列表 */
    getList() {
      this.loading = true;
      listErpSupplier(this.queryParams).then((response) => {
        this.ErpSupplierList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 供方类型选择
    handleSupplierTypeChange() {
      if (this.form.supplierType == 2) {
        this.form.inventoryType = 2;
      }
    },
    // 表单重置
    reset() {
      this.form = {
        supplierId: null,
        supplierNo: null,
        supplierName: null,
        supplierType: null,
        inventoryType: null,
        companyName: null,
        productType: null,
        linkman: null,
        linkmanPhone: null,
        bankName: null,
        bankAccount: null,
        bankCard: null,
        taxpayerNo: null,
        supplierAddress: null,
        remark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.supplierId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.form.supplierNo =
        "GY" + new Date().getTime();
      this.title = "添加供应商管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const supplierId = row.supplierId || this.ids;
      getErpSupplier(supplierId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改供应商管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.supplierId != null) {
            updateErpSupplier(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addErpSupplier(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal
        .confirm('是否确认删除供应商名称为"' + row.supplierName + '"的数据项？')
        .then(function () {
          return delErpSupplier(row.supplierId);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "erp/ErpSupplier/export",
        {
          ...this.queryParams,
          exportIds: this.ids?.join(',')
        },
        `供应商_${new Date().getTime()}.xlsx`
      );
    },
    /** 设置商品 */
    handleSetCommodity(row) {
      this.$router.push(
        "/ErpSupplier/set-commodity/erpSupplier/" + row.supplierId
      );
    },
  },
};
</script>
<style scoped>
.el-select {
  width: 100%;
}
</style>
